package com.ruoyi.eqmonitor.controller;

import java.util.List;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysDept;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.eqmonitor.domain.OptAreaSetting;
import com.ruoyi.eqmonitor.service.IOptAreaSettingService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 片区设置
 * @author ruoyi
 * @date 2022-12-05
 */
@RestController
@RequestMapping("/areaSetting")
public class OptAreaSettingController extends BaseController{
    
    @Autowired
    private IOptAreaSettingService optAreaSettingService;

    /**
     * 片区设置列表
     * @param optAreaSetting 片区设置查询参数
     * @return 表格分页数据对象
     */
    @GetMapping("/list")
    @ApiOperation("片区设置列表")
    public TableDataInfo list(OptAreaSetting optAreaSetting){
        startPage();
        List<OptAreaSetting> list = optAreaSettingService.selectOptAreaSettingList(optAreaSetting);
        return getDataTable(list);
    }

    /**
     * 查询片区设置列表（排除节点）
     */
    @ApiOperation("查询片区设置列表（排除节点）")
    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id) {
        List<OptAreaSetting> optAreaSettings = optAreaSettingService.selectOptAreaSettingList(new OptAreaSetting());
        optAreaSettings.removeIf(d -> d.getId().intValue() == id); //|| ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + "")
        return success(optAreaSettings);
    }

    /**
     * 根据id查询片区详细信息
     * @param id 片区设置主键id
     * @return 通用data数据对象
     */
    @ApiOperation("根据id查询片区详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(optAreaSettingService.selectOptAreaSettingById(id));
    }

    /**
     * 新增片区设置
     * @param optAreaSetting 片区设置对象
     * @return 通用返回对象
     */
    @ApiOperation("新增片区设置")
    @Log(title = "片区设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptAreaSetting optAreaSetting){
    	optAreaSetting.setCreateTime(new Date());
    	optAreaSetting.setCreateBy(SecurityUtils.getUsername());
        return toAjax(optAreaSettingService.save(optAreaSetting));
    }

    /**
     * 修改片区设置
     * @param optAreaSetting 片区设置对象
     * @return 通用返回对象
     */
    @ApiOperation("修改片区设置")
    @PutMapping
    public AjaxResult edit(@RequestBody OptAreaSetting optAreaSetting){
    	optAreaSetting.setUpdateTime(new Date());
    	optAreaSetting.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(optAreaSettingService.updateById(optAreaSetting));
    }

    /**
     * 根据ids删除片区设置传入@PathVariable的格式类似为/1,2,3,4,5时,会自动转为数组
     */
    @ApiOperation("根据ids删除片区设置")
    @Log(title = "片区设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(optAreaSettingService.removeByIds(Arrays.asList(ids)));
    }
    
    /**
     * 导出片区设置列表
     */
    @ApiOperation("导出片区设置列表")
    @Log(title = "片区设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptAreaSetting optAreaSetting){
        List<OptAreaSetting> list = optAreaSettingService.selectOptAreaSettingList(optAreaSetting);
        ExcelUtil<OptAreaSetting> util = new ExcelUtil<OptAreaSetting>(OptAreaSetting.class);
        util.exportExcel(response, list, "片区设置数据");
    }
    
}
